package com.txw.eventbus_fragment_demo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.txw.eventbus_fragment_demo.R;

import org.greenrobot.eventbus.EventBus;


public class ItemListFragment extends ListFragment {


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //注册EventBus
        EventBus.getDefault().register(this);
        //EventBus.getDefault().isRegistered()



    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //开启线程加载数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    //模拟延时
                    Thread.sleep(2000);

                    //发布事件，在后台线程发的事件
                    //EventBus.getDefault().post(new ItemListFragment(Item.ITEMS));

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_item_list, container, false);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //解注册
        EventBus.getDefault().unregister(this);

    }
}
